package com.jfirer.validator.util;

import com.jfirer.baseutil.bytecode.util.BytecodeUtil;

import javax.validation.ParameterNameProvider;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DefaultParameterNameProvider implements ParameterNameProvider
{

    @Override
    public List<String> getParameterNames(Constructor<?> constructor)
    {
        String[] strings = BytecodeUtil.parseConstructorParamNames(constructor);
        if (strings != null)
        {
            return Arrays.asList(strings);
        }
        List<String> list   = new ArrayList<String>();
        int          length = constructor.getParameterTypes().length;
        for (int i = 0; i < length; i++)
        {
            list.add(String.valueOf(i));
        }
        return list;
    }

    @Override
    public List<String> getParameterNames(Method method)
    {
        String[] strings = BytecodeUtil.parseMethodParamNames(method);
        if (strings != null)
        {
            return Arrays.asList(strings);
        }
        List<String> list   = new ArrayList<String>();
        int          length = method.getParameterTypes().length;
        for (int i = 0; i < length; i++)
        {
            list.add(String.valueOf(i));
        }
        return list;
    }
}
